//
//  LHPageScrollView.m
//  Login
//
//  Created by tgs on 2017/2/16.
//  Copyright © 2017年 tgs. All rights reserved.
//

#import "LHPageScrollView.h"

@interface LHPageScrollView()<UIScrollViewDelegate>

@property(nonatomic, strong) UIScrollView *scrollview;
@property(nonatomic, strong) CALayer *lineLayer;
@property (nonatomic, strong) NSMutableArray *buttonArray;
@property (nonatomic, assign) NSInteger currentIndex;

@end

@implementation LHPageScrollView


- (instancetype)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        _itemWidth = 60.f;
        _lineColor = [UIColor lightGrayColor];
        _font = [UIFont systemFontOfSize:17.0];
        _selectFont =  [UIFont systemFontOfSize:22.0];
        _textColor = [UIColor blackColor];
        _selectTextColor = [UIColor blueColor];
        _scale = false;
        self.buttonArray = [[NSMutableArray alloc] init];
    }
    return self;
}

#pragma mark - setter
- (void)setItemWidth:(CGFloat)itemWidth
{
    _itemWidth = itemWidth;
}

- (void)setLineColor:(UIColor *)lineColor
{
    _lineColor = lineColor;
}

- (void)setTextColor:(UIColor *)textColor
{
    _textColor = textColor;
}

- (void)setSelectTextColor:(UIColor *)selectTextColor
{
    _selectTextColor = selectTextColor;
}

- (void)setScale:(BOOL)scale
{
    _scale = scale;
}


- (void)setArrTitiles:(NSArray *)arrTitiles {
    _arrTitiles = arrTitiles;

    
    self.scrollview.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height - LineHeight);
    self.scrollview.bounces = NO;

    if (arrTitiles.count) {
        for (NSInteger i=0; i<arrTitiles.count; i++) {
            
            //当自定义宽度小于平分title的宽度时，使用根据屏幕宽度进行等分宽度
            CGFloat width = self.scrollview.bounds.size.width / arrTitiles.count;
            if (self.itemWidth <= width) {
                self.itemWidth = width;
            }
            
            UIButton *item =  [[UIButton alloc] initWithFrame:CGRectMake(i * self.itemWidth, 5, self.itemWidth, self.frame.size.height - LineHeight -10)];
            item.backgroundColor = [UIColor clearColor];
            [item setTitle:arrTitiles[i] forState:UIControlStateNormal];
            item.titleLabel.font = self.font;
            item.tag = i + 1;
            [item setTitleColor:self.textColor forState:UIControlStateNormal];
            [item addTarget:self action:@selector(itemClick:) forControlEvents:UIControlEventTouchUpInside];
            if (i == 0) {
                item.titleLabel.font = self.selectFont;
                [item setTitleColor:self.selectTextColor forState:UIControlStateNormal];
                item.selected = NO;
            }
            [self.scrollview addSubview:item];
            [self.buttonArray addObject:item];
            
           
        }
        self.selectView = [[UIView alloc] initWithFrame:CGRectMake(self.itemWidth/2-3, self.frame.size.height/2+15, 6, 6)];
        self.selectView.backgroundColor = self.selectTextColor;
        [self.scrollview addSubview:self.selectView];
        [self.scrollview bringSubviewToFront:self.selectView];
        
    }
    
    self.lineLayer.frame = CGRectMake(0, self.scrollview.bounds.size.height + 10, self.scrollview.bounds.size.width, LineHeight);
    
     self.scrollview.contentSize = CGSizeMake(arrTitiles.count * self.itemWidth, self.frame.size.height - LineHeight);
}



#pragma mark - response Action
- (void)itemClick:(UIButton *)sender
{

    UIButton *item = sender;
    if ([self.delegate respondsToSelector:@selector(selectedIndex:)]) {
        [self.delegate selectedIndex:item.tag -1];
    }
}


-(void)selectIndex:(NSInteger)index{
    UIButton *item = (UIButton *)[self.scrollview viewWithTag:index+1];
    item.selected = !item.selected;
    CGFloat width = self.scrollview.bounds.size.width / self.arrTitiles.count;
    
    //判断当自定义item宽度大于平分的时候，调整选中item的显示位置
    if (self.itemWidth > width) {
        float offsetX = (item.center.x - self.scrollview.frame.size.width/2.0) > 0 ? (item.center.x - self.scrollview.frame.size.width/2.0) : 0;
        float maxOffsetX = self.scrollview.contentSize.width - self.scrollview.frame.size.width;
        if (offsetX < maxOffsetX) {
            [self.scrollview setContentOffset:CGPointMake(offsetX, 0) animated:true];
        } else {
            [self.scrollview setContentOffset:CGPointMake(maxOffsetX, 0) animated:true];
        }
    }
    
    for (UIButton *item in self.buttonArray) {
        if (item.selected) {
            [item setTitleColor:self.selectTextColor forState:UIControlStateNormal];
            item.selected = NO;
            [UIView animateWithDuration:0.3 animations:^{
                item.titleLabel.font = self.selectFont;
                self.selectView.frame = CGRectMake(index*self.itemWidth + self.itemWidth/2-3, self.frame.size.height/2+15, 6, 6);
            }];
        }
        else{
            [item setTitleColor:self.textColor forState:UIControlStateNormal];
            [UIView animateWithDuration:0.3 animations:^{
                item.titleLabel.font = self.font;
            }];
        }
    }
}

#pragma mark - getter
- (UIScrollView *)scrollview
{
    if (!_scrollview) {
        _scrollview = [[UIScrollView alloc] init];
        _scrollview.delegate = self;
        _scrollview.showsVerticalScrollIndicator = false;
        _scrollview.showsHorizontalScrollIndicator = false;
        [self addSubview:_scrollview];
    }
    return _scrollview;
}

- (CALayer *)lineLayer
{
    if (!_lineLayer) {
        _lineLayer = [CALayer layer];
        [self.layer addSublayer:_lineLayer];
        _lineLayer.backgroundColor = self.lineColor.CGColor;
    }
    return _lineLayer;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
